/*******************************************************************************
 * Level Master 2000
 * Copyright (C) 2010 Christian Herzog
 * For more information see http://code.google.com/p/levelmaster2000/
 *
 * This file is licensed under the terms of the MIT license, see
 * http://www.opensource.org/licenses/mit-license.php for further information.
 ******************************************************************************/

package com.zog 
{
    import Box2D.Common.Math.b2Vec2;
    import com.pblabs.box2D.Box2DSpatialComponent;
	import com.pblabs.box2D.CollisionEvent;
	import com.pblabs.engine.core.ITickedObject;
	import com.pblabs.engine.core.InputMap;
	import com.pblabs.engine.core.ObjectTypeManager;
	import com.pblabs.engine.core.ProcessManager;
	import com.pblabs.engine.entity.EntityComponent;
	import com.pblabs.engine.entity.PropertyReference;
	import com.pblabs.engine.resource.*;
	import com.pblabs.engine.PBE;

    import  com.pblabs.engine.PBUtil;
	import flash.geom.Point;
	import flash.media.Sound;

	import com.pblabs.rendering2D.SpriteSheetRenderer;
	import com.pblabs.animation.AnimatorComponent;

	public class CarController extends EntityComponent implements ITickedObject 
    {
		
        [TypeHint(type="flash.geom.Point")]
		public var velocityReference:PropertyReference;
        
        public var isActive:Boolean = true;

		public function get input():InputMap {
			return _inputMap;
		}

		public function set input(value:InputMap):void {
			_inputMap = value;

			if (_inputMap!=null){
				_inputMap.mapActionToHandler("GoLeft", _OnLeft);
				_inputMap.mapActionToHandler("GoRight", _OnRight);
                _inputMap.mapActionToHandler("GoForward", _OnForward);
                _inputMap.mapActionToHandler("GoBackward", _OnBackward);
			}
		}

		public function onTick(tickRate:Number):void 
        {
            var speed:Number = 240;

            if(forward != 0.0)
            {
                var newX:Number = forward * Math.cos(spatialReference.body.GetAngle());
                var newY:Number = forward * Math.sin(spatialReference.body.GetAngle());
                
                spatialReference.linearVelocity = new Point( newX * speed,  newY * speed);
            }
            else
            {
                spatialReference.linearVelocity = new Point(0,0);
            }
            
            spatialReference.angularVelocity = 0;
            
            spatialReference.rotation += forward * (-_left + _right) * 3;
		}

		public function onInterpolateTick(factor:Number):void {
		}

		protected override function onAdd():void {
			PBE.processManager.addTickedObject(this);
		}

		protected override function onRemove():void {
			_inputMap.destroy();
			PBE.processManager.removeTickedObject(this);
		}

        private function _OnForward(value:Number):void 
        {
            forward = value;
        }
        private function _OnBackward(value:Number):void 
        {
            forward = -value;
        }
        
		private function _OnLeft(value:Number):void 
        {
            _left = value;
        }
                
		private function _OnRight(value:Number):void 
        {
            _right = value;
		}

        //[EditorData(referenceType="componentReference")]
        public var spatialReference:Box2DSpatialComponent = null;
        
		private var _sound:Sound = null;
		private var _inputMap:InputMap;
		private var _left:Number = 0;
		private var _right:Number = 0;
        private var forward:Number = 0;
	}
}